package org.baitiaojun.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.baitiaojun.dao.CheckGroupCheckItemMapper;
import org.baitiaojun.dao.CheckItemMapper;
import org.baitiaojun.pojo.CheckGroupCheckItemKey;
import org.baitiaojun.pojo.CheckItem;
import org.baitiaojun.service.CheckItemService;
import org.baitiaojun.util.MessageConstant;
import org.baitiaojun.vo.PageResult;
import org.baitiaojun.vo.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @类名 CheckItemServiceImpl
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2022/5/24 19:34
 * @版本 1.0
 */

@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Autowired
    private CheckGroupCheckItemMapper checkGroupCheckItemMapper;

    @Override
    public int saveCheckItem(CheckItem checkItem) {
        return checkItemMapper.insertSelective(checkItem);
    }

    @Override
    public PageResult<CheckItem> queryCheckItemBySplitPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryCondition = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);

        List<CheckItem> checkItems = checkItemMapper.selectCheckItemListByCondition(queryCondition);

        PageResult<CheckItem> pageResult = new PageResult<>();
        pageResult.setRows(checkItems);
        pageResult.setTotal(new PageInfo<>(checkItems).getTotal());
        return pageResult;

    }

    @Transactional(
            isolation = Isolation.REPEATABLE_READ,
            propagation = Propagation.REQUIRED,
            rollbackFor = {
                RuntimeException.class
            }
    )
    @Override
    public void removeCheckItemById(Integer id) throws Exception {
        List<CheckGroupCheckItemKey> checkGroupCheckItemKeys = checkGroupCheckItemMapper.selectCheckGroupCheckItemByCheckItemId(id);

        if (!ObjectUtils.isEmpty(checkGroupCheckItemKeys)) {
            for (CheckGroupCheckItemKey checkGroupCheckItemKey : checkGroupCheckItemKeys) {
                int checkGroupCheckItemRes = checkGroupCheckItemMapper.deleteCheckGroupCheckItemMapperById(checkGroupCheckItemKey.getCheckgroupId());
                if (checkGroupCheckItemRes == 0) {
                    throw new Exception(MessageConstant.DELETE_CHECKGROUP_CHECKITEM_FAIL);
                }
            }
        }

        int checkItemRes = checkItemMapper.deleteByPrimaryKey(id);
        if (checkItemRes == 0) {
            throw new Exception(MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    @Override
    public CheckItem queryCheckItemById(Integer id) {
        return checkItemMapper.selectCheckItemById(id);
    }

    @Override
    public int editCheckItemById(CheckItem checkItem) {
        return checkItemMapper.updateByPrimaryKeySelective(checkItem);
    }

    @Override
    public List<CheckItem> queryCheckItemList() {
        return checkItemMapper.selectCheckItemList();
    }
}
